package com.zwj.batch.config.test;

import com.zwj.batch.config.springBatch.listener.step.MyStepListener;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * 步骤，流程控制DEMO
 * @author 张文接
 * @version 1.0
 * @createTime 2023/6/24 15:05
 */
//@Configuration
public class BatchConfig12 {
    @Resource
    private JobBuilderFactory jobBuilderFactory;
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("step-multi-job")//给job命名
                .incrementer(new RunIdIncrementer()) //增量器
                .start(firstStep()).on("*").to(successStep())
                .from(firstStep()).on(ExitStatus.FAILED.getExitCode()).to(failStep())
                .end()
                .build();
    }

    @Bean
    public Step firstStep() {
        return stepBuilderFactory.get("firstStep")//给step命名
                .tasklet(firstTasklet())//注入具体要执行的逻辑
                .build();//构建
    }

    @Bean
    public Step successStep() {
        return stepBuilderFactory.get("successStep")//给step命名
                .tasklet(successTasklet())//注入具体要执行的逻辑
                .build();//构建
    }

    @Bean
    public Step failStep() {
        return stepBuilderFactory.get("failStep")//给step命名
                .tasklet(failTasklet())//注入具体要执行的逻辑
                .build();//构建
    }

    @Bean
    public Tasklet firstTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("========================first=====================");
                throw new RuntimeException("操作失败");
//                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet successTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("========================success=====================");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet failTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("========================fail=====================");
                return RepeatStatus.FINISHED;
            }
        };
    }


    @Bean
    public MyStepListener stepListener() {
        return new MyStepListener();
    }

}
